package com.example.controller;


import com.example.po.Sys_Order;
import com.example.po.Sys_User;
import com.example.service.Sys_CarService;
import com.example.service.Sys_CityService;
import com.example.service.Sys_OrderService;
import com.example.vo.CarVo;
import com.example.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

import java.util.List;

/**
 * @auter czcjq
 * @date 2022/5/17 15:43
 */
@Controller
@RequestMapping("/order")
public class Sys_OrderController {
    @Autowired
    private Sys_OrderService sys_orderService;
    @Autowired
    private Sys_CarService sys_carService;
    @Autowired
    private Sys_CityService sys_cityService;

    @ResponseBody
    @RequestMapping(value = "/submit", method = RequestMethod.GET)
    public CarVo addOrder(int getid, int backid, int cid, Double oprice, HttpSession session) {

        Sys_User user = (Sys_User) session.getAttribute("SysUser");
        CarVo ar = new CarVo();
        if (user != null) {
            int uid = user.getId();
            Sys_Order order = new Sys_Order(0, cid, uid, getid,
                    backid, oprice, "已预定");
            Sys_Order order1 = sys_orderService.addOrder(order);
            if (order1 != null) {
                ar.setMsg("订单提交成功");
                ar.setCode(1);
            }
        } else {
            ar.setCode(-1);
            ar.setMsg("你还未登录");
        }
        return ar;
    }

    @ResponseBody
    @RequestMapping(value = "/findones", method = RequestMethod.GET)
    public List<OrderVo> listOnesOD(HttpSession session, int page) {
        Sys_User user = (Sys_User) session.getAttribute("SysUser");
        List<OrderVo> orderVOList = new ArrayList<>();
        if (user != null) {
            int uid = user.getId();
            List<Sys_Order> orderList = sys_orderService.listByUid(uid);
            for (Sys_Order o : orderList) {
                String carname = sys_carService.findOneByID(o.getCid()).getName();
                String getcity = sys_cityService.findCityById(o.getGetid()).getName();
                String backcity = sys_cityService.findCityById(o.getBackid()).getName();
                OrderVo ovo = new OrderVo(o.getId(), carname, getcity, backcity, o.getOprice(), o.getStatus());
                orderVOList.add(ovo);
            }
            /*System.out.println("订单集合"+orderList);*/
        }
        return orderVOList;
    }

    @ResponseBody
    @RequestMapping(value = "/delorder", method = RequestMethod.GET)
    public CarVo delOrder(int id) {
        CarVo ar = new CarVo();
        boolean b = sys_orderService.delOrder(id);
        if (b) {
            ar.setMsg("订单删除成功");
            ar.setCode(1);

        } else {
            ar.setCode(-1);
            ar.setMsg("订单删除失败");

        }
        System.out.println("删除功能"+ar);
        return ar;
    }

}
